package com.krtech.wecard.module.ramble.controller;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.krtech.wecard.framework.exception.BusinessException;
import com.krtech.wecard.framework.utils.LoginUtil;
import com.krtech.wecard.module.ramble.entity.*;
import com.krtech.wecard.module.ramble.service.RambleDrawChanceService;
import com.krtech.wecard.module.ramble.service.RambleHexiaoLogService;
import com.krtech.wecard.module.ramble.service.RambleHexiaoPeopleService;
import com.krtech.wecard.module.sys.entity.SysUser;
import com.krtech.wecard.module.sys.service.SysUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/rambleHexiao")
@Slf4j
@Api(tags = "佳得乐漫步-核销")
public class RambleHexiaoLogController {

    @Autowired
    private RambleDrawChanceService drawChanceService;

    @Autowired
    private RambleHexiaoLogService hexiaoLogService;

    @Autowired
    private SysUserService userService;

    @Autowired
    private RambleHexiaoPeopleService hexiaoPeopleService;

    @GetMapping("/hexiao")
    @ApiOperation(value = "核销")
    public RambleHexiaoLog hexiao(String code){
        SysUser userInfo = LoginUtil.getUserInfo();

        RambleHexiaoPeople hexiaoPeople = hexiaoPeopleService.getOne(new QueryWrapper<RambleHexiaoPeople>().eq(RambleHexiaoPeople.COL_PHONE, userInfo.getPhone()));
        if (hexiaoPeople == null){
            throw new BusinessException("您不是核销人员，不能进行核销");
        }

        //增加核销日志,判断是否核销了
        RambleDrawChance drawChance = drawChanceService.getOne(new QueryWrapper<RambleDrawChance>().eq(RambleDrawChance.COL_HEXIAO_CODE, code));
//        if (drawChance.getHasHexiao() == 1){
//            return null;
//        }
//
        SysUser user = userService.getOne(new QueryWrapper<SysUser>().eq(SysUser.COL_PHONE, drawChance.getPhone()));
//
//        drawChance.setHasHexiao(1);
//        drawChanceService.updateById(drawChance);

        //判断是否有这条哦核销记录了
        RambleHexiaoLog hasHexiao = hexiaoLogService.getOne(new QueryWrapper<RambleHexiaoLog>().eq(RambleHexiaoLog.COL_FK_CHANCE_ID, drawChance.getId()));
        if (hasHexiao == null){
            //记录入核销日志
            RambleHexiaoLogVo hexiaoLog = new RambleHexiaoLogVo();
            hexiaoLog.setCreatedTime(new Date());
            hexiaoLog.setHexiaoPhone(userInfo.getPhone());
            hexiaoLog.setBeNickName(user.getNickName());
            hexiaoLog.setBeHexiaoPhone(user.getPhone());
            hexiaoLog.setBeOpenId(user.getMipOpenId());
            hexiaoLog.setFkChanceId(drawChance.getId());

            hexiaoLog.setBeChance(drawChance.getDrawChance());
            hexiaoLog.setAlreadyChance(0);
            hexiaoLog.setCanChance(drawChance.getDrawChance());

            hexiaoLogService.save(hexiaoLog);

            return hexiaoLog;
        }else {
            //第二次核销
            //计算可用次数
            RambleHexiaoLogVo hexiaoLog = new RambleHexiaoLogVo();
            BeanUtil.copyProperties(hasHexiao,hexiaoLog);

            Integer beChance = drawChance.getDrawChance() - hexiaoLog.getBeChance();

            hexiaoLog.setAlreadyChance(hexiaoLog.getBeChance());
            hexiaoLog.setCanChance(beChance);
            hexiaoLog.setBeChance(drawChance.getDrawChance());
            hexiaoLogService.updateById(hexiaoLog);
            return hexiaoLog;
        }


    }
}
